# Author: HZT
# Date: 2023/8/28
import unittest
from config.config_loader import config
from utils import UtilsDriver, login_with_session
from page.web_sk_top import WebSkTop
from utils import DBUtil, get_db_sxye, get_db_ye
import pyperclip
import time


class TestWebSkTop(unittest.TestCase):
    driver = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.driver = UtilsDriver.get_web_driver()
        # 实例化
        cls.skTop = WebSkTop(cls.driver)
        # 打开网页
        cls.driver.get(config.URL)
        # 登录
        login_with_session('system2', cls.driver)

    @classmethod
    def tearDownClass(cls):
        # 在所有测试用例执行完毕后，关闭浏览器
        # UtilsDriver.get_web_driver().quit()
        pass

    # 余额正确（验证余额，转账码是否复制功能）
    def test01_sk_top(self):
        # 获取页面余额
        ye = self.skTop.sk_top_get_ye()
        # 获取数据库余额
        db_ye = get_db_ye(config.CREDENTIALS["username_sk"])
        # 断言
        self.assertEqual(ye.replace(',', ''), db_ye)

    # 验证授信余额
    def test02_sk_top(self):
        # 数据库授信余额
        db_scye = get_db_sxye(config.CREDENTIALS["username_sk"])
        # 获取页面授信余额
        sxye = self.skTop.sk_top_get_sxye()
        # 断言
        self.assertEqual(sxye.replace(',', ''), db_scye)

    # 验证转账码
    def test03_sk_top(self):
        # 获取转账码
        zzm = self.skTop.sk_top_get_zzm()

        # 点击转账码
        self.skTop.sk_top_click_zzm()
        time.sleep(2)
        zzmtc = self.skTop.sk_top_get_zzmtc()
        clipboard_text = pyperclip.paste()
        # 断言是否复制到剪切板以及弹窗
        self.assertEqual("复制成功", zzmtc) and self.assertEqual(zzm, clipboard_text)
        time.sleep(1)
        # 点击OK
        self.skTop.sk_top_click_zzmqr()

    # 转账   不太好做参数化
    # @parameterized.expand(read_json("sk_hl.json"))
    # def test04_sk_top(self, code, money, expect):
    #     # 点击转账
    #     self.skTop.sk_top_click_zz()
    #     # 输入收款码
    #     self.skTop.sk_top_input_skrzzm(code)
    #     # 点击转账文字，失焦
    #     self.skTop.sk_top_click_zzwz()
    #     self.skTop.sk_top_input_je(money)
    #     time.sleep(1)
    #     self.skTop.sk_top_click_ljzz()
    #     if code == 'CK64E41DFB2CC20' and money == '500':
    #         time.sleep(1)
    #         self.skTop.sk_top_click_zzqd()
    #     # 获取转账弹窗文本
    #     zztc = self.skTop.sk_top_get_zztc()
    #     print(expect)
    #     self.assertEqual(zztc, expect)

    # 转账成功（转账码和金额有效）
    def test04_sk_top(self):
        self.skTop.sk_top_click_zz()
        self.skTop.sk_top_zz("CK64E41DFB2CC20", "500", "转账！", self.driver)
        self.skTop.sk_top_click_zzqd()
        # 获取转账弹窗文本
        time.sleep(0.5)
        zztc = self.skTop.sk_top_get_zztc()
        self.assertIn("转账成功", zztc)

    # 转账失败（转账码错误，金额有效）
    def test05_sk_top(self):
        self.skTop.sk_top_zz("CK64E41DFB2CC201", "500", "转账！", self.driver)
        # 获取转账弹窗文本
        time.sleep(0.5)
        zztc = self.skTop.sk_top_get_zztc()
        self.assertIn("请输入正确的转账码", zztc)
        time.sleep(1)

    # 转账失败（转账码正确，为自己的转账码，金额有效）
    def test06_sk_top(self):
        self.skTop.sk_top_zz("SK64E442563BE14", "500", "转账！")
        # 获取转账弹窗文本
        time.sleep(2)
        zztc = self.skTop.sk_top_get_zztc()
        self.assertIn(zztc, ("不能转账给自己", "请输入正确的转账码"))

    # 转账失败（转账码正确，金额无效）
    def test07_sk_top(self):
        self.skTop.sk_top_zz("CK64E41DFB2CC20", "0", "转账！")
        # 获取转账弹窗文本
        time.sleep(2)
        zztc = self.skTop.sk_top_get_zztc()
        self.assertIn(zztc, "请输入大于0最多保留两位小数金额")

    # 转账失败（转账码正确，金额超出余额）
    def test08_sk_top(self):
        self.skTop.sk_top_zz("CK64E41DFB2CC20", "5000000", "转账！")
        time.sleep(1)
        self.skTop.sk_top_click_zzqd()
        # 获取转账弹窗文本
        time.sleep(0.5)
        zztc = self.skTop.sk_top_get_zztc()
        self.assertIn(zztc, "余额不足")

    # 转账失败（转账码正确，金额不输入）
    def test09_sk_top(self):
        self.skTop.sk_top_zz("CK64E41DFB2CC20", " ", "转账！")
        # 获取转账弹窗文本
        zztc = self.skTop.sk_top_get_zztc()
        self.assertIn(zztc, "请输入大于0最多保留两位小数金额")

    # 更新成功（正确的名称和密码）
    def test10_sk_top(self):
        self.skTop.sk_top_sz("子拓收卡", "qwe123", "qwe123")
        # 获取用户名
        username = config.CREDENTIALS["username_sk"]
        # 获取真实数据
        name = DBUtil.select_one("SELECT name FROM `admin_users` where username= '{}'".format(username))
        self.assertEqual("子拓收卡", str(name[0]))
